//
//  BecomeAFreeMemberViewController.m
//  Rockval
//
//  Created by TuanNT on 5/21/14.
//  Copyright (c) 2014 TNL. All rights reserved.
//
#import "LoginViewController.h"
#import "BecomeAFreeMemberViewController.h"
#define kAllScoresUnorderedArray @"Countries"
@interface BecomeAFreeMemberViewController () {
	NSMutableArray *listCountry;
	NSMutableDictionary *listChosen;
	int codeCountry;
}

@end

@implementation BecomeAFreeMemberViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
		// Custom initialization
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
    
    //	NSString *plistPath = [[NSBundle mainBundle] pathForResource:kAllScoresUnorderedArray ofType:@"plist"];
    //	NSMutableDictionary *values = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    //	listChosen = values;
    listChosen = [DataManager getFullContries];
	NSLog(@"arrayValues = %@", listChosen);
	codeCountry = 254;
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
- (void)setDataRequest {
	ObSignUpRequest *request = [[ObSignUpRequest alloc] init];
	request.gender = self.btnGender.selected ? @"Male" : @"Female";
	request.firstName = self.txfFirstName.text;
	request.lastName = self.txfLastName.text;
	request.email = self.txfYourEmail.text;
	request.passWord = self.txfNewPassword.text;
	request.rePassWord = self.txfNewPassword.text;
	request.telephone = self.txfDayPhone.text;
	request.role = 1;
	request.company = @"";
	request.website = @"";
	request.address = self.txfTemp.text;
	request.postcode = self.txfZipCode.text;
	request.state = 0;
	request.country = codeCountry;
	request.city = @"";
	[[APIManager sharedInstance] signUpWithObject:request andBlock: ^(BOOL isSucsess, NSError *error) {
	    [self hideHud];
	    if (isSucsess) {
	        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"RockvalStoryboard" bundle:[NSBundle mainBundle]];
	        LoginViewController *loginView = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
	        [self.navigationController pushViewController:loginView animated:YES];
	        [loginView.txtEmail setText:self.txfYourEmail.text];
	        [loginView.txtPassword setText:self.txfNewPassword.text];
		}
	    else {
	        [UIAlertView showAlertViewWithMessage:error.description];
		}
	}];
}

- (IBAction)btnRegisterKeyPress:(id)sender {
	if (self.txfFirstName.text.length == 0) {
		[UIAlertView showAlertViewWithMessage:@"FirstName is Invalid!"];
		return;
	}
	else if (self.txfLastName.text.length == 0) {
		[UIAlertView showAlertViewWithMessage:@"LastName is Invalid!"];
		return;
	}
	else if (self.txfYourEmail.text.length == 0) {
		[UIAlertView showAlertViewWithMessage:@"YourEmail is Invalid!"];
		return;
	}
	else if (self.txfNewPassword.text.length == 0) {
		[UIAlertView showAlertViewWithMessage:@"NewPassword is Invalid!"];
		return;
	}
	else if (self.txfZipCode.text.length == 0) {
		[UIAlertView showAlertViewWithMessage:@"ZipCode is Invalid!"];
		return;
	}
	else if (self.txfDayPhone.text.length == 0) {
		[UIAlertView showAlertViewWithMessage:@"DayPhone is Invalid!"];
		return;
	}
	else if (self.txfTemp.text.length == 0) {
		[UIAlertView showAlertViewWithMessage:@"Address is Invalid!"];
		return;
	}
	else {
		[self showHud];
		[self setDataRequest];
	}
}

- (IBAction)btnCheckGenderKeyPress:(id)sender {
	if (self.btnGender.selected) {
		[self.btnGender setSelected:NO];
	}
	else {
		[self.btnGender setSelected:YES];
	}
}

- (IBAction)btnCheckKeyPress:(id)sender {
	if (self.btnCheck.selected) {
		[self.btnCheck setSelected:NO];
	}
	else {
		[self.btnCheck setSelected:YES];
	}
}

- (IBAction)btnChosenCountryKeyPress:(id)sender {
	[self dismissKeyboard];
	[ActionSheetStringPicker showPickerWithTitle:@"Select Country"
	                                        rows:[listChosen allValues]
	                            initialSelection:0
	                                   doneBlock: ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue)
     {
         codeCountry = selectedIndex;
         [self.btnChosenCountry setTitle:[NSString stringWithFormat:@"   %@", selectedValue] forState:UIControlStateNormal];
     }
     
	                                 cancelBlock: ^(ActionSheetStringPicker *picker) {}
     
	                                      origin:self.view];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
	[self dismissKeyboard];
}

@end
